<?php
class BuySell {
	//Check the user buy
	public function checkUserBuy($totalprice, $userid)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM user INNER JOIN usermoney ON (user.userid = usermoney.userid) WHERE (user.UserID ='$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$row = mysql_fetch_array($result);
		if ($totalprice > intval($row['Money']))
		{
			echo "Can not buy";
		}
		else
		{
			echo "Can buy";
		}
	}
	//Check the user sell
	function checkUserSell($stockid, $userid, $amount)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM userstock WHERE (UserID ='$userid' AND StockID = '$stockid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRows = mysql_num_rows($result);
		if ($nRows == 0)
		{
			echo "User do not have the stock";
		}
		else
		{
			$row = mysql_fetch_array($result);
			if ($amount > intval($row['Amount']))
			{
				echo "Not enough to sell";
			}
			else
			{
				echo "Can sell";
			}
		}
	}
	//------------------------------------------------------------------------------------------------------------------------------------
	//Check user already bought the stock in BUYLO table
	function isActionLO($stockid, $userid, $isBuy)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM buylo WHERE (stockid ='$stockid') AND (userid = '$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		if ($nRow > 0)	return false;
		
		$sql = "SELECT * FROM selllo WHERE (stockid ='$stockid') AND (userid = '$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		if ($nRow > 0)	return false;
		return true;
	}

	//BUY LO action
	function actionBuyLO($stockid, $userid,	$amount, $code, $price)
	{
		include_once  $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlLO.php';
		if ($this->isActionLO($stockid, $userid, true))
		{
			$control = new ControlLO();
			$control->buyLO($stockid, $userid, $amount, $code, $price);
			$liveBoard = new LiveBoard();
			$liveBoard->setLiveBoard_LO($code);
		}
	}


	//Sell LO action
	function actionSellLO($stockid, $userid, $amount, $code, $price)
	{
		include_once  $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlLO.php';
		if ($this->isActionLO($stockid, $userid, false))
		{
			$control = new ControlLO();
			$control->sellLO($stockid, $userid, $amount, $code, $price);
			$liveBoard = new LiveBoard();
			$liveBoard->setLiveBoard_LO($code);
		}
	}
	//-------------------------------------------------------------------------------------------------
	//BUY MP action
	function actionBuyMP($stockid, $userid,	$amount, $code)
	{
		include_once $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlMP.php';
		if ($this->isActionLO($stockid, $userid))
		{
			$control = new ControlMP();
			$control->buyMP($stockid, $userid, $amount, $code);
			$liveBoard = new LiveBoard();
			$liveBoard->setLiveBoard_LO($code);
		}
	}


	//Sell MP action
	function actionSellMP($stockid, $userid, $amount, $code)
	{
		include_once $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlMP.php';
		if ($this->isActionLO($stockid, $userid))
		{
			$control = new ControlMP();
			$control->sellMP($stockid, $userid, $amount, $code);
			$liveBoard = new LiveBoard();
			$liveBoard->setLiveBoard_LO($code);
		}
	}
	//---------------------------------------------------------------------------------------------------------------------------------------
	//Check user already bought the stock in BUYLO table
	function isBuyAT($stockid, $userid)
	{
		include_once $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM buyat WHERE (stockid ='$stockid') AND (userid = '$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		if ($nRow > 0)	return false;
		return true;
	}
	//BUY AT action
	function actionBuyAT($stockid, $userid,	$amount, $code, $price)
	{
		include_once $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlAT.php';
		if ($this->isBuyAT($stockid, $userid))
		{
			$control = new ControlAT();
			$control->buyAT($stockid, $userid, $amount, $code, $price);
			$liveBoard = new LiveBoard();
			$liveBoard->setLiveBoard_AT($code);
		}
	}

	//Check user already sold the stock in SELLLO table
	function isSellAT($stockid, $userid)
	{
		include_once $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM sellat WHERE (stockid ='$stockid') AND (userid = '$userid')";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		if ($nRow > 0)	return false;
		return true;
	}

	//Sell AT action
	function actionSellAT($stockid, $userid, $amount, $code, $price)
	{
		include_once $_SERVER['DOCUMENT_ROOT'].'scue'.'/controller/ControlAT.php';
		if ($this->isSellAT($stockid, $userid))
		{
			$control = new ControlAT();
			$control->sellAT($stockid, $userid, $amount, $code, $price);
			$liveBoard = new LiveBoard();
			$liveBoard->setLiveBoard_AT($code);
		}
	}
	//-----------------------------------------------------------------------------------------------------------------------------------
}
?>